package com.reapal.utils;

import com.reapal.model.DbConfig;
import lombok.extern.slf4j.Slf4j;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

import java.io.*;
import java.util.*;

/**
 * @ClassName : SnakeYamlUtilis
 * @Author : jack-cooper
 * @Description:
 * @Date : 2018-08-28 09:22
 */
@Slf4j
public class SnakeYamlUtilis implements ConfigDao{

    private static SnakeYamlUtilis snakeYamlUtilis;

    private static Yaml yaml;

    private static File file  = new File("database.yml");

    static {
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                log.error("create new database.yml error:",e);
            }
        }
    }

    private SnakeYamlUtilis(){
        DumperOptions options = new DumperOptions();
        options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
        yaml = new Yaml(options);
        System.out.println("database.yml路径==================> " + file.getAbsolutePath());
    }

    public static ConfigDao getInstance(){
        if(snakeYamlUtilis == null){
            snakeYamlUtilis = new SnakeYamlUtilis();
        }
        return snakeYamlUtilis;
    }


    /**
     * add
     * @param dbConfig
     * @return
     */
    @Override
    public boolean addDbconfig(DbConfig dbConfig){
        Map<String, DbConfig> map = this.getDbConfigMaps();
        try {
            map.put(dbConfig.getDbName(), dbConfig);
            yaml.dump(map, new OutputStreamWriter(new FileOutputStream(file)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * modify
     * @param dbConfig
     * @return
     */
    @Override
    public boolean modifyDbconfig(DbConfig dbConfig){
        return this.addDbconfig(dbConfig);
    }

    /**
     * remove
     * @param key
     * @return
     */
    @Override
    public boolean deleteDbconfig(String key){
        Map<String, DbConfig> map = this.getDbConfigMaps();
        try {
            map.remove(key);
            yaml.dump(map, new OutputStreamWriter(new FileOutputStream(file)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * getById
     * @param key
     * @return
     */
    @Override
    public DbConfig getDbconfigByKey(String key) {
        Map<String, DbConfig> map = this.getDbConfigMaps();
        return map.get(key);
    }


    /**
     * list
     * @return
     */
    @Override
    public List<DbConfig> getAllDbconfig(){
        List<DbConfig> list = new ArrayList<>();
        Map<String, DbConfig> map = this.getDbConfigMaps();
        map.forEach((k, v) -> list.add(v));
        return list;
    }

    /**
     * 加载yml -> map
     * @return
     */
    private Map<String,DbConfig> getDbConfigMaps(){
        Map<String, DbConfig> map = new HashMap<>();
        try {
            map = yaml.loadAs(new InputStreamReader(new FileInputStream(file)), Map.class);
            if (map == null){
                map = new HashMap<>();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return map;
    }


}
